<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="/plugins/select2/select2.css" />
	<link rel="stylesheet" href="/plugins/select2/select2-bootstrap.css" />
	<script src="/plugins/select2/select2.min.js" type="text/javascript"></script>
    <script>
		$(function () {
			$("#typeId").select2();
			$("#typeId1").select2();
			show(0);
		})

		function show(id) {
			//告知增加时的父级id
			$("#parentId").val(id);
			$.ajax({
				url:"/itemCat/ItemCatList",
				type:"post",
				dataType:"json",
				data:{parentId:id},
				success:function (result) {
					if (result.code==200){
						var data = result.data;
						var tbody="";
						for (var i=0;i<data.length;i++){
							tbody += '<tr>';
							tbody += '<td><input  type="checkbox" name="box" value="'+data[i].id+'"></td>';
							tbody += '<td>'+data[i].id+'</td>';
							tbody += '<td>'+data[i].name+'</td>';
							tbody += '<td>'+data[i].typeId+'</td>';
							tbody += '<td class="text-center">';
							tbody += '<button type="button" class="btn bg-olive btn-xs" onclick="show('+data[i].id+')">查询下级</button>';
							tbody += '<button type="button" class="btn bg-olive btn-xs" onclick="getDataById('+data[i].id+')" data-toggle="modal" data-target="#updateditModal">修改</button>';
							tbody += '</td>';
							tbody += '</tr>';
						}
						$("#tbody").html(tbody);
						getItem(id);
					}else {
						var tbody = result.data;
						$("#tbody").html(tbody);
					}

				},
				error:function () {
					alert("查询失败");
				}
			})
		}
		//获取分类的id和名字
		function getItem(id) {
           $.ajax({
			   url:"/itemCat/getItemById/"+id,
			   type:"post",
			   data:"json",
			   success:function (result) {
			    //	console.log("111"+result)
			      var data = result.data;
			    //  console.log("****"+data)
			      console.log("---"+data.name);
				   var list = $("<li id='" + id + "'><a href='javascript:show(" + id + ")'>" + data.name + "</a></li>");
				   //appendTo() 方法在被选元素的结尾插入指定内容
                   list.appendTo($("#item"))
                   //根据id删除后面的元素
				   $("#"+id+"").nextAll().remove();
				 //  console.log("#"+id+"");

				   //设置增加修改上街商品分类
				   var li = $("#item").find("li").find("a");
				   var text="";
				   $(li).each(function (i,e) {
				    	//console.log("++++"+e.text);
							if (i==li.length-1){
								text +=e.text;
							}else {
								text += e.text+">>"
							}
				   })
				   //增加分类抬头
                     $("#saveName").html(text);
				   //修改分类
				    $("#updateName").html(text);
			   },
			   error:function () {
					alert("系统异常")
			   }
		   })
		}

		//删除
		function deleteItemCat() {
           var boxs=$("[type=checkbox]:checked");
           var ids=[];
           $(boxs).each(function (i,e) {
				ids.push(e.value);
		   })
			if (ids.length<=0){
				alert("请选择要删除的数据");
				return;
			}
			$.ajax({
				url:"/itemCat/deleteItemCat",
				type:"post",
				dataType:"json",
				data:{ids:ids},
				success:function (result) {
                   alert("删除成功");
                   location.reload();
				},
				error:function () {
					alert("删除失败");
				}
			})
		}

		//增加
		function insert() {
			//获取父级id  //需要知道增加时的父级id
            var parentId=$("#parentId").val();
           // alert(parentId)
            var name = $("#saveName1").val();
            //获取select2的数据
			var items = $("#typeId").select2('data');
			var typeId=items.id;
			$.ajax({
				url:"/itemCat/insertItemCat",
				type:"post",
				dataType:"json",
				data:{name:name,parentId:parentId,typeId:typeId},
				success:function (result) {
					alert("增加成功");
					location.reload();
				},
				error:function () {
					alert("增加失败");
				}
			})
		}
		
		//回显
		function getDataById(id) {
           $.ajax({
			   url:"/itemCat/getItemCat/"+id,
			   type:"post",
			   dataType:"json",
			   success:function (result) {
				   if (result.code==200){
				   	$("#updateItemId").val(result.data.id);
				   	$("#updateName1").val(result.data.name);
				   	//select2回显
				     var typeId=result.data.typeId;
				     // console.log(typeId);
				     $("#typeId1").val(typeId).trigger('change');
				   }
			   },
			   error:function () {
					alert("回显失败")
			   }
		   })
		}

		//修改
		function updateItemCat() {
             var id = $("#updateItemId").val();
             var name = $("#updateName1").val();
             var typeIds = $("#typeId1").select2('data');
             var typeId = typeIds.id;
             $.ajax({
				 url:"/itemCat/updateItemCat",
				 type:"post",
				 dataType:"json",
				 data:{id:id,name:name,typeId:typeId},
				 success:function (result) {
					 alert("修改成功");
					 location.reload();
				 },
				 error:function () {
					alert("修改失败");
				 }
			 })

		}
	</script>
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb" id="item">
                        		<li id="0">
		                        	<a href="javascript:show(0)" >顶级分类列表</a>
		                        </li>

	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteItemCat()" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" ><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>

			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>
									     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbody">
			                         <!-- <tr >
			                              <td><input  type="checkbox" ></td>			                              
				                          <td>982</td>
									      <td>吊坠/项链</td>									    
									      <td>
									      	11    
									      </td>									      
		                                  <td class="text-center">		                                     
		                                      <button type="button" class="btn bg-olive btn-xs" >查询下级</button> 		                                     
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           
		                                  </td>
			                          </tr>
									  <tr >
			                              <td><input  type="checkbox"> </td>			                              
				                          <td>983</td>
									      <td>手镯/手链/脚链</td>									    
									      <td>
									      	11    
									      </td>									      
		                                  <td class="text-center">		                                     
		                                      <button type="button" class="btn bg-olive btn-xs" >查询下级</button> 		                                     
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           
		                                  </td>
			                          </tr>
									  <tr >
			                              <td><input  type="checkbox" ></td>			                              
				                          <td>984</td>
									      <td>戒指/耳饰</td>									    
									      <td>
									      	11    
									      </td>									      
		                                  <td class="text-center">		                                     
		                                      <button type="button" class="btn bg-olive btn-xs" >查询下级</button> 		                                     
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           
		                                  </td>
			                          </tr>-->
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                      
						
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
					<input type="hidden" id="parentId">
				</tr>
				<tr>
		      		<td>上级商品分类</td>
		      		<td>
		      		  <div id="saveName"></div>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input id="saveName1"  class="form-control" placeholder="商品分类名称">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
		      		<td>	      		
		      			<!--<input select2 config="options['type_template']" placeholder="商品类型模板" class="form-control" type="text"/>-->
						<select id="typeId" name="typeId" style="width: 100%">
							<option th:each="item:${templates}" th:value="${item.id}">[[${item.name}]]</option>
						</select>
		      		</td>		      		      		
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="insert()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 修改窗口 -->
  <div class="modal fade" id="updateditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel">商品分类编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td><input type="text" id="updateItemId"></td>
					  </tr>
					  <tr>
						  <td>上级商品分类</td>
						  <td>
							  <div id="updateName"></div>
						  </td>
					  </tr>
					  <tr>
						  <td>商品分类名称</td>
						  <td><input id="updateName1" name="name"  class="form-control" placeholder="商品分类名称">  </td>
					  </tr>
					  <tr>
						  <td>类型模板</td>
						  <td>
<!--
							  <input select2 config="options['type_template']" placeholder="商品类型模板" class="form-control" type="text"/>
-->
							  <select id="typeId1" name="typeId" style="width: 100%">
								  <option th:each="item:${templates}" th:value="${item.id}">[[${item.name}]]</option>
							  </select>
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateItemCat()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>
</html>